package com.java.jdbc;

import java.sql.*;

public class JDBCDemo {

    public static final String URL = "jdbc:mysql://localhost:3306/mydb?serverTimezone=GMT";
    public static final String USER = "root";
    public static final String PASSWORD = "123456";
    public static void testConnection(){
        //通过驱动管理获得数据库连接
        try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD)){
            System.out.println(connection);
            //获得命令对象
            Statement statement = connection.createStatement();
            System.out.println(statement);
            //执行命令
//            int rows = statement.executeUpdate(
//                    "insert into student(name,age,gender,cellphone,address) values('王五',23,'男','15645557788','南京')");
//            if(rows > 0){
//                System.out.println("添加成功");
//            }
            //执行查询命令
            ResultSet resultSet = statement.executeQuery("select * from student");
            //遍历结果集
            while(resultSet.next()){
                System.out.println("=============================");
                System.out.println("id:"+resultSet.getInt("id"));
                System.out.println("姓名:"+resultSet.getString("name"));
                System.out.println("年龄:"+resultSet.getInt("age"));
                System.out.println("性别:"+resultSet.getString("gender"));
                System.out.println("手机号:"+resultSet.getString("cellphone"));
                System.out.println("地址:"+resultSet.getString("address"));
            }
            resultSet.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void testPreparedStatement(){
        //通过驱动管理获得数据库连接
        try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD)){
            System.out.println(connection);
            //获得预编译命令对象
            PreparedStatement statement = connection.prepareStatement("insert into student(name,age,gender,cellphone,address) " +
                    "values(?,?,?,?,?)");
            //设置参数
            statement.setString(1,"王大五");
            statement.setInt(2,23);
            statement.setString(3,"男");
            statement.setString(4,"15645557799");
            statement.setString(5,"北京");
            //执行命令
            int rows = statement.executeUpdate();
            if(rows > 0){
                System.out.println("添加成功");
            }
            statement = connection.prepareStatement("select * from student");
            //执行查询命令
            ResultSet resultSet = statement.executeQuery();
            //遍历结果集
            while(resultSet.next()){
                System.out.println("=============================");
                System.out.println("id:"+resultSet.getInt("id"));
                System.out.println("姓名:"+resultSet.getString("name"));
                System.out.println("年龄:"+resultSet.getInt("age"));
                System.out.println("性别:"+resultSet.getString("gender"));
                System.out.println("手机号:"+resultSet.getString("cellphone"));
                System.out.println("地址:"+resultSet.getString("address"));
            }
            resultSet.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
//        testConnection();
        testPreparedStatement();
    }
}
